package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uha.reggie.DTO.DishDto;
import com.uha.reggie.DTO.SetmealDto;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.Category;
import com.uha.reggie.entity.Dish;
import com.uha.reggie.entity.Setmeal;
import com.uha.reggie.service.CategoryService;
import com.uha.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;




    @GetMapping("/page")
    public ReggieResult<Page> page(int page,int pageSize,String name){

        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo, setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return ReggieResult.success(setmealDtoPage);
    }




    @PostMapping
    public ReggieResult<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return ReggieResult.success("新增套餐成功~");
    }

    /**
     * 查询套餐
     * @param id id
     * @return ReggieResult<SetmealDto>
     */

    @GetMapping("/{id}")
    public ReggieResult<SetmealDto> get(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        return  ReggieResult.success(setmealDto);
    }



    @PutMapping
    public ReggieResult<String> edit(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);

        return ReggieResult.success("修改套餐成功~");
    }



    /**
     * 起售停售---批量起售停售
     * @param ids  ids
     * @return null
     */
    @PostMapping("/status/{status}")
    public ReggieResult<String> status(@PathVariable Integer status, Long[] ids){
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.in(Setmeal::getId,ids);

        setmealService.update(setmeal,queryWrapper);
        return  ReggieResult.success("已更改~");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public ReggieResult<List<Setmeal>> LIST(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return ReggieResult.success(list);
    }

    /**
     * 获取套餐全部菜品
     * @param id id
     * @return
     */
    @GetMapping("/dish/{id}")
    public ReggieResult<SetmealDto> dishget(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        /**
         * ···································································有问题···································································
         */

        return ReggieResult.success(setmealDto);

    }



    /**
     * 关联删除
     * @param ids ids 批量删除
     * @return null
     */
    @DeleteMapping
    public ReggieResult<String> delete(Long[] ids){
        //LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper();
        //
        //wrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus,1);
        //
        //List<Setmeal> list = setmealService.list(wrapper);
        //if (list.size() == 0){
        //    setmealService.deleteWithDish(ids);
        //    return  ReggieResult.success("删除成功~");
        //}

        setmealService.deleteWithDish(ids);

        //return  ReggieResult.error("套餐停售才能删除~");
        return  ReggieResult.success("删除成功~");

    }



}
